package org.example.jida;


import cn.com.jit.assp.dsign.DSign;

import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Locale;

/**
 * 3.1.3Detach签名/Detach验签名(原型1) ok
 * 将字符串进行加密后返回加密字符串值
 */
public class DetachSignTest {

    /**
     * ***************************Main方法测试类***************************************
     */
    public static void main(String[] args) throws MalformedURLException {

        DSign signDs = new DSign();
        //通过获取配置文件读取信息
        signDs.initConfig("E:/root/epolicy/epolicyModel/cssconfig/2.properties");
        //1.获取原文-直接赋串
        byte[] srccode = "123".getBytes();// 需要签名的原文
        System.out.println("srccode = " + Arrays.toString(srccode));

        /*detach签名*/
        String signReturn = null;
        signReturn = signDs.detachSign("", srccode);
        System.out.println("srccode = " + Arrays.toString(srccode));

        if (signReturn != null) { //返回字符串不为空代表签名成功
            System.out.println("*************Detach签名成功！*******************");
        } else {
            System.out.println("错误号为：" + signDs.getErrorCode());
            System.out.println("错误描述为: " + signDs.getErrorMessage());
        }
        /**********************************detach验签名**************************/
        DSign vSignDs = new DSign();
        //通过获取配置文件读取信息
        vSignDs.initConfig("E:/root/epolicy/epolicyModel/cssconfig/2.properties");
        System.out.println("srccode = " + Arrays.toString(srccode));

        long verifyResult = vSignDs.verifyDetachedSign(signReturn.getBytes(), "1234".getBytes());
        if (verifyResult == 0) { //返回0代表验证成功
            System.out.println("************Detach验证成功,返回证书信息*****************");
            System.out.println("版本号:" + vSignDs.getCertInfo("VS", 3, ""));
            System.out.println("颁发者:" + vSignDs.getCertInfo("VS", 1, ""));
            System.out.println("证书主题:" + vSignDs.getCertInfo("VS", 0, ""));
            System.out.println("验签返回的摘要算法为：" + vSignDs.getRndigestAlgID());

            //签名结果带时间戳
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new DateFormatSymbols(Locale.SIMPLIFIED_CHINESE));
            String signedTimeStr = "";
            if (vSignDs.getTsaTime() != null) {
                signedTimeStr = formatter.format(vSignDs.getTsaTime());
            }
            System.out.println("时间: " + signedTimeStr);
            System.out.println("证书subjectdn: " + vSignDs.getTsaCertInfo("subjectdn"));
            System.out.println("证书issure： " + vSignDs.getTsaCertInfo("issuerdn"));
            System.out.println("证书SN： " + vSignDs.getTsaCertInfo("serialnumber"));
        } else {
            System.out.println("错误号为:" + vSignDs.getErrorCode());
            System.out.println("错误描述:" + vSignDs.getErrorMessage());
        }
    }
}